import Mitt from 'mitt'

const emitter = new Mitt()

// 构造广播白名单
const BusEvents = [
	'require-signin',
	'loading-start',
	'loading-end'
]

const BusWhiteList = BusEvents.reduce((obj, type) => {
	obj[type] = true
	return obj
}, {})

function showWarn (type) {
	console.warn(`[model/bus] type ${type} is not in whitelist`)
}

const Bus = {
	on (type, fn) {
		if (BusWhiteList[type]) {
			emitter.on(type, fn)
		} else {
			showWarn(type)
		}
	},
	off (type, fn) {
		emitter.off(type, fn)
	},
	emit (type, arg) {
		if (BusWhiteList[type]) {
			emitter.emit(type, arg)
		} else {
			showWarn(type)
		}
	}
}

export default Bus
